package com.machine.filling.net.ws.network;

import android.util.Log;

import com.google.gson.Gson;
import com.machine.filling.net.ws.req.WsBaseReq;

import okhttp3.WebSocket;

/**
 * Created by sperfan on 2018/3/1.
 *
 * @描述:
 */

public class APIWsManager {
    private static final String TAG = "APIWsManager";
    private static APIWsManager instance;
    private static WebSocket mWebSocket;
    private String token;
    private String machineId;
    private static WebSocketUtil webSocketUtil;

    private APIWsManager(String token, String machineId) {
        this.token = token;
        this.machineId = machineId;
    }

    public static APIWsManager getInstance(String token, String machineId) {
        if (instance == null) {
            instance = new APIWsManager(token, machineId);
            webSocketUtil = WebSocketUtil.getInstance();
            webSocketUtil.initWebSocket(token, machineId);
            mWebSocket = webSocketUtil.getmWebSocket();
        }
        return instance;
    }

    /**
     * 设置Websocket 的返回监听
     *
     * @param iWebSocketCallback
     */
    public void setWsCallback(IWebSocketCallback iWebSocketCallback) {
        webSocketUtil.setiWebSocketCallback(iWebSocketCallback);
    }

    public static WebSocket getmWebSocket() {
        return mWebSocket;
    }

    public void sendWebSocketMsg(WebSocket webSocket, WsBaseReq wsReq) {
        String reqStr = new Gson().toJson(wsReq);
        Log.d(TAG, "\n WS请求===>: " + reqStr);
        webSocket.send(reqStr);
    }

    public void sendWebSocketMsg(WsBaseReq wsReq) {
        String reqStr = new Gson().toJson(wsReq);
        Log.d(TAG, "\n WS请求===>: " + reqStr);
        mWebSocket.send(reqStr);
    }


}
